﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using YouHP.Model;
using YouHP.BLL;

namespace web.admin
{
    public partial class SendSystemEmail : System.Web.UI.Page
    {
        protected List<UserInfo> ReceiverList;
        protected List<UserInfo> ReceiverList2 = new List<UserInfo>();

        //System.Threading.Timer t;
        protected void Page_Load(object sender, EventArgs e)
        {

           // 得到发送对象初步列表
            ReceiverList = UserBLL.Instance.GetSystemEamilReceiverList();

            foreach (UserInfo item in ReceiverList)
            {

                //对初步对象进行筛选
                SystemEmail se = UserBLL.Instance.GetSystemEmail(item.Id, 1);
                if (se.ReceiverId == -1)
                {
                    ReceiverList2.Add(item);
                }
                else
                {
                    TimeSpan ts = DateTime.Now.Subtract(se.LatestSendTime);
                    if (ts.Days > 7)
                    {
                        ReceiverList2.Add(item);
                    }
                }
            }
            //发送邮件
            foreach(UserInfo item in ReceiverList2)
            {
                PatientRecordInfo DiseaseData = PatientBLL.Instance.GetPatientRecordInfo(item.Id);
                string mailSubject = "亲爱的" + item.UserName + "，优医网提醒您，该更新您的病情系统了！";
                string mailBody = "<html>\r\n<head></head>\r\n<body><div>\r\n";
                mailBody += "<div style=\"overflow:hidden;width: 840px;\"><img src=\"http://www.youhp.com/support/Alpha/images/top_bg.jpg\"style=\" overflow:hidden; \" /></div>";
                mailBody += "<div class=\"content\" style=\"background-image:http://www.youhp.com/support/Alpha/img/dl_bg_03.jpg\" >";
                mailBody += "<p style=\"width: 840px;\">您已经有超过30天没有更新您的病情系统了，这不利于<a href=\"http://www.youhp.com/Pages/Alpha/PatientList.aspx?dszid=" + DiseaseData.DiseaseId + "\">您和你一样的病友</a>更多的了解您，<a href=\"http://www.youhp.com/Pages/Alpha/SymptomHistory/Index.aspx\">现在就去更新</a></p>";
                mailBody += "<li>您的指标有变化吗？<a href=\"http://www.youhp.com/Pages/Alpha/IndicatorHistory/Index.aspx\">更新</a></li>";
                mailBody += "<li>您的症状有变化吗？<a href=\"http://www.youhp.com/Pages/Alpha/SymptomHistory/Index.aspx\">更新</a></li>";
                mailBody += "<li>您使用的药物有变化吗？<a href=\"http://www.youhp.com/Pages/Alpha/TreatmentHistory/Index.aspx\">更新</a></li>";
                mailBody += "<li>您的体重有变化吗？<a href=\"http://www.youhp.com/Pages/Alpha/WeightHistory/Index.aspx\">更新</a></li>";
                mailBody += "</div></div></body></html>";

                YouHP.Utility.Utils.sendMail(item.Email, mailSubject, mailBody, true);

                //更新系统邮件
                SystemEmail se2 = new SystemEmail();
                se2.ReceiverId = item.Id;
                se2.LatestSendTime = DateTime.Now;
                se2.EmailType = 1;
                if (UserBLL.Instance.GetSystemEmail(item.Id, 1).Id != -1)
                {
                    UserBLL.Instance.UpdateSystemEmail(se2);
                }
                else
                {
                    UserBLL.Instance.AddSystemEmail(se2);
                }
            }
        }
    }
}
